
//#include "copy/Wife.h"

#include <iostream>
#include <sstream>
//#include <bitset>
//#include "initTable/People.h"
//#include "youyuan/globalFuncFriend/Wife.h"
//#include "duotai/Cat.h"
//#include "filerw/FilerWr.h"
//#include "duotai/Cpu.h"
//#include "duotai/IntelCpu.h"
//#include "duotai/IntelMemory.h"
//#include "duotai/IntelVedioCard.h"
//#include "duotai/Computer.h"
//
#include "copy/Wife.h"
#include "copy/WorkCopy.h"
#include "union/UnionPerson.h"
#include "sqllite/WSqlite.h"
#include "websocket/wskserver.h"
#include "websocket/httpserver.h"
#include "websocket/myfriends.h"

using namespace std;

//
////全局函數
//void kissMe(Wife *wife) {
//    cout << wife->kiss << endl;
//}
//
//void sayPeopleSelf(People &people) {
//    cout << people.age << endl;
//    cout << people.name << endl;
//}

//返回右值引用
//WorkCopy getWorkCopy2() {
//    //直接返回苟傲函数，但是没有返回引用，所以返回的是一个右值
//    return WorkCopy();
//}
//
//WorkCopy getWorkCopy() {
//    WorkCopy workCopy;
//    return workCopy;
//}

//Wife getWife2() {
//    Wife wife;
//    return wife;
//}


void testSql() {
    WSqlite ws;
    //string dbpath = "/opt/zhouyu.db";
    string dbpath = "/mnt/d/zhouyu.db";
    //创建数据库和表
    /*cout<<"create db"<<endl;
    static const char *myfriends_feild = "ID integer primary key autoincrement,Name string,Age integer,Major string";
    int dbcreate = ws.CreateDbFile(dbpath.c_str());
    if (dbcreate != SQLITE_OK) {
        cout << "create db error:" << dbcreate<<endl;
    } else {
        cout<<"createtable start"<<endl;
        string table = "myfriends";
        int res=ws.CreateTable(table, myfriends_feild);
        cout<<"createtable:"<<endl;
    }*/


    int opendb = ws.Opendb(dbpath);
    stringstream querysql;
    querysql << "SELECT * from myfriends;";
    /* vector<string> colnames;
     vector<vector<string>> datas;
     int queryData = ws.QueryData(querysql.str(), colnames, datas);
     if (SQLITE_OK == queryData) {
         cout << "query return:" << queryData << endl;
         if (!colnames.empty() && !datas.empty()) {
             for (int i = 0; i < colnames.size(); ++i) {
                 cout << colnames[i].c_str() << "\t";
             }
             cout << endl;
             for (int i1 = 0; i1 < datas.size(); i1++) {
                 for (int i2 = 0; i2 < datas[0].size(); i2++) {
                     cout << datas[i1][i2] << "\t";
                 }
                 cout << endl;
             }
         }
     }*/

    //查询结果
    vector<map<std::string, std::string>> results;
    int queryData = ws.QueryData2(querysql.str(), results);
    if (SQLITE_OK == queryData) {
        cout << "query results2:" << results.size() << endl;
        if (!results.empty()) {
            for (int i = 0; i < results.size(); ++i) {
                map<string, string> &rowmap = results[i];
                for (const auto &item: rowmap) {
                    cout << item.first << ":" << item.second << "   ";
                }
                cout << endl;
            }


            cout << "####################### db to obj  #######################" << endl;
            //数据转化为对象
            myfriends myfrienddb;
            myfriends *db = myfrienddb.db_to_obj(results);
            cout << "db.id:" << db->getId() << endl;
            cout << "db.name:" << db->getName() << endl;
            cout << "db:age:" << db->getAge() << endl;
            cout << "db.major:" << db->getMajor() << endl;
            free(db);
            db = nullptr;

            cout << "####################### db to objs  #######################" << endl;
            const vector<myfriends> &vector = myfriends::db_to_objs(results);
            for (const auto &item: vector) {
                cout << item.getId() << " " << item.getName() << " " << item.getAge() << " " << item.getMajor() << endl;
            }

            cout << "####################### db to objsp  #######################" << endl;
            const ::vector<myfriends *> &objsp = myfriends::db_to_objsp(results);
            for (auto *item: objsp) {
                cout << item->getId() << " " << item->getName() << " " << item->getAge() << " " << item->getMajor()
                     << endl;
            }
            for (auto *item: objsp) {
                free(item);
            }

        }
    }

    //插入数据库
    /*if (SQLITE_OK != opendb) {
        cout << "can't open db:" << dbpath << endl;
    } else {
        stringstream insertsql;
        insertsql << "insert into myfriends(Name, Age, Major)";
        insertsql << "values('laoban', '44', 'fkc');";
        cout << "insert table" << endl;
        int insert = ws.Insert(insertsql.str());
        if (SQLITE_OK != insert) {
            cout << "can't insert table myfriends db:" << dbpath << endl;
        }
    }*/
}

int decimal_bcd_code(int decimal) {
    // sum返回的BCD码
    int sum = 0;

    for (int i = 0; decimal > 0; i++) {
        sum |= ((decimal % 10) << (4 * i));

        decimal /= 10;
    }

    return sum;
}

int bcd_decimal_code(int bcd) {
    // sum返回十进制，c每次翻10倍
    int sum = 0, c = 1;

    for (int i = 1; bcd > 0; i++) {
        if (i >= 2) {
            c *= 10;
        }

        sum += (bcd % 16) * c;
        // 除以16同理与十进制除10将小数点左移一次，取余16也同理
        bcd /= 16;
    }

    return sum;
}


int main() {
//
//   /* int a0=1<<0;
//    int a1=1<<1;
//    int a2=1<<2;
//    int a3=1<<3;
//
//    cout<<"a0:"<<a0<<",bina:"<<bitset<32>(a0-1)<<endl;
//    cout<<"a1:"<<a1<<",bina:"<<bitset<32>(a1-1)<<endl;
//    cout<<"a2:"<<a2<<",bina:"<<bitset<32>(a2-1)<<endl;
//    cout<<"a3:"<<a3<<",bina:"<<bitset<32>(a3-1)<<endl;*/
//
//    /*cout<<"a0,jizhi:"<<oct<<a0<<endl;//八进制
//    cout<<"a0,16jinzhi:"<<hex<<a0<<endl;	//十六进制*/

    /* char *name = new char('z');
     Wife w1(name, 28);
     //默认的拷贝函数就是浅拷贝,浅拷贝会导致引用数据类型的内存重复释放
     Wife w2(w1);
     cout << "p1's age: " << w1.age << " name: " << *w1.name << endl;
     cout << "p2's age: " << w2.age << " name: " << *w2.name << endl;*/
//
//    /* //默认无参
//     People people;
//     people.getAge();
//     cout<<people.getAge()<<endl;
//     sayPeopleSelf(people);
//
//     //初始化列表:有参
//     People people1={"zhouyu",30};
//     sayPeopleSelf(people1);
//
//     //初始化列表:有常量成员
//     People people3={"zhouyu",33,"zhouguilin,baijinxiu"};
//     sayPeopleSelf(people3);
//     people3.peopleMsg();
//     return 0;*/
//
//    /*Animal *animal = new Cat(30,"zhouyu");
//    animal->sayMself();
//    //如果父类的析构不是纯析构，会导致无法释放子类实例内存
//    delete animal;*/
//
//    /*Cpu * intelcpu=new IntelCpu();
//    Memory* memory=new IntelMemory();
//    VideoCard* vedioCard=new IntelVedioCard();
//    Computer* computer=new Computer(intelcpu,vedioCard,memory);
//    computer->work();
//    delete computer;*/
//
//    //文件读写
//   /* FilerWr * filerWr=new FilerWr();
//    //filerWr->wite();
//    filerWr->read();
//    delete filerWr;*/
//
//
//    myadd()
    //常量左值引用是一个万能引用类型，它可以接受左值、右值、常量左值和常量右值。
    // const WorkCopy &workCopy2 = getWorkCopy2();
    //使用右值引用接受返回值
//    WorkCopy &&workCopy2 = getWorkCopy2();
//    WorkCopy workCopy1=getWorkCopy();
//    UnionPerson p1(1);
//    UnionPerson p2(222.222);
//    UnionPerson p3(333, 1339219);
//    p1.print();
//    p2.print();
//    p3.print();

//    wskserver wbskserver;
//    wbskserver.run();

    //testSql();

    /* httpserver httpserver;
     httpserver.run();*/

    /*int bcdCode = decimal_bcd_code(21);
    unsigned char bcd = bcdCode;
    printf("%#hx", bcdCode);*/

//    char data = 0x00;
//    //0011 1000
//    char datas[] = {0, 0, 0, 1, 1, 1, 0, 0};
//    for (int i = 0; i < 8; ++i) {
//        /*if(datas[i]){
//            data|=0x80;
//        }
//        if(i!=7){
//            //右移一位，把最高位空出来 最后一次不要右移
//            data>>=1;
//        }*/
//
//        //先接受低位，再接受高位
//        data >>= 1;
//        if (datas[i]) {
//            data |= 0x80;
//        }
//    }
//    printf("%hx", data);
//    std::cout << std::dec << data << std::endl;
    char insertSql[500];
    sprintf(insertSql, "INSERT INTO PROTOC_SETTING_DETAILS VALUES(%d,%s,%s,%s,"
                       "%d,%d,%d,%s,"
                       "%s,%s,%s)",
            2, "zt", "zbc", "sab",
            3, 4, 5, "uyt",
            "ray", "muy", "out");
    printf("%s", insertSql);  // 输出字符串
}
